{extend name="common/form" /}
{block name="form" /}
<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
    <div class="widget am-cf">
        <div class="widget-head am-cf">

            <div class="widget-title am-fl">添加商品</div>
            <div class="widget-function am-fr">
                <a href="javascript:;" class="am-icon-cog"></a>
            </div>
        </div>
        <div class="widget-body am-fr">
            <form class="am-form tpl-form-border-form tpl-form-border-br">
                <input type="hidden" name="id" value="{$info.id|default=''}">
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        商品名 </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="goods_title" value="{$info.goods_title|default=''}" required="required"
                               class="tpl-form-input" placeholder="请填写商品名">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        所需积分 </label>
                    <div class="am-u-sm-9">
                        <input type="number" name="integral" value="{$info.integral|default=''}" required="required"
                               class="tpl-form-input" placeholder="请填写所需积分">
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        原价 </label>
                    <div class="am-u-sm-9">
                        <input type="number" name="old_price" value="{$info.old_price|default=''}" required="required"
                               class="tpl-form-input" placeholder="请填写原价">
                    </div>
                </div>
                <div class="am-form-group  am-form-file">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        图片
                    </label>
                    <div class="am-u-sm-9">
                        <i class="am-icon-camera am-icon-btn"></i>
                        <p>请选择图片..</p>

                        <input type="file" value="{$info.goods_images|default=''}" multiple accept="image/*" onchange="up(this)" id="photoFile">
                        <img style="width: 200px" id="images" src="{$info.goods_images|default=''}">
                    </div>
                </div>

                <div class="am-form-group  am-form-file">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        商品图片
                    </label>
                    <div class="am-u-sm-9">
                        <input type="text" name="goods_images" value="{$info.goods_images|default=''}" required="required" class="tpl-form-input" placeholder="请上传">
                    </div>
                </div>



                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        商品详情 </label>
                    <div class="am-u-sm-9">
                        <textarea id="content" name="content"/>{$info.content|default=''}</textarea>
                    </div>
                </div>
                <div class="am-form-group">
                    <label for="user-name" class="am-u-sm-3 am-form-label">
                        库存 </label>
                    <div class="am-u-sm-9">
                        <input type="number" name="stock" value="{$info.stock|default=''}" required="required"
                               class="tpl-form-input" placeholder="请填写库存">
                    </div>
                </div>

                <div class="am-form-group">
                    <div class="am-u-sm-9 am-u-sm-push-3">
                        <button type="button" onclick="Updata()" class="am-btn am-btn-primary tpl-btn-bg-color-success" >
                        提交
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
    <div class="am-modal-dialog">
        <div class="am-modal-bd">
        </div>
        <div class="am-modal-footer">
            <span class="am-modal-btn">确定</span>
        </div>
    </div>
</div>
{/block}
{block name='script' /}
<script src="/static/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
    CKEDITOR.editorConfig = function( config ) {
        config.filebrowserUploadUrl = "{:url('up/upImage1')}";
    };
    CKEDITOR.replace('content',{height:"700px"});
    //修改
    CKEDITOR.instances['content'].on('fileUploadResponse', function (evt) {
        var data = evt.data
        var xhr = data.fileLoader.xhr
        var status = xhr.status
        if (status === 200) {
            var res = JSON.parse(xhr.response)
            data.url = res.data;
        } else {
            evt.cancel()
        }
    });
    function up(e) {
        var target = $(e);
        var file;
        if(target[0].files && target[0].files[0] ) {
            file = target[0].files[0];
        }
        if(file) {
            var reader = new FileReader();
            reader.onload = function(evt){
                var imgstr = evt.target.result; //这就是base64字符串
                document.getElementById('images').setAttribute('src',imgstr);
            };
            reader.readAsDataURL(file);
            var index = file.name.lastIndexOf(".");
            var suffix = file.name.substr(index+1);
            var keys = Math.random().toString().slice(-10)+"."+suffix;
            var formData = new FormData();
            formData.append('upload',document.getElementById('photoFile').files[0]);
            $.AMUI.progress.start();
            $.ajax({
                url: "{:url('/Up/upImage')}",
                type: 'POST',
                data: formData,
                dataType: 'JSON',
                cache: false,
                processData: false,
                contentType: false
            }).done(function(ret) {
                if(ret.code ==0){
                    $('input[name="goods_images"]').val(ret.data);
                    $.AMUI.progress.done();
                }else{
                    $('.am-modal-bd').html(ret.msg)
                    $('#my-alert').modal({
                        relatedElement: this,
                    })
                }


            }).error(function (xhr) {
                $('.am-modal-bd').html(xhr.responseJSON.error)
                $('#my-alert').modal({
                    relatedElement: this
                })
            })
        }
    }
    function Updata() {
        $('#content').val(CKEDITOR.instances['content'].getData())
        $.post('',$('form').serialize(),function (res) {
//            is_ajax =1;
            if(res.code!=0){
                layer.alert(res.msg,{icon:5})
            }else{
                layer.alert('操作成功!',{icon:6},function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    window.parent.location.reload();
                    parent.layer.close(index);//关闭当前页

                })
            }
        },'json')

    }
</script>
{/block}
{extend}
